package com.fone.player.activity.personal.adapter;

import java.util.Date;
import java.util.List;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.fone.player.R;
import com.fone.player.cache.ImageDownloadModule;
import com.fone.player.entity.NotificationBean;
import com.fone.player.util.FoneUtil;

/**
 * 通知消息中 通知和消息公用的adapter
 * 
 * @author JasonZue
 * @since 2014-4-21
 */
public class MessageAdapter extends BaseAdapter {

	private List<NotificationBean> itemList;
	private Context mContext;
	private List<Boolean> mIsManagement;

	public MessageAdapter(Context mContext, List<NotificationBean> listData,
			List<Boolean> mIsManagement) {
		this.mContext = mContext;
		this.itemList = listData;
		this.mIsManagement = mIsManagement;
	}

	@Override
	public int getCount() {
		return itemList.size();
	}

	@Override
	public Object getItem(int position) {
		return itemList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = View.inflate(mContext,
					R.layout.personal_notify_video_list_item, null);
			holder.notification_item_ll = (LinearLayout) convertView
					.findViewById(R.id.notification_item_ll);
			holder.sign_unread_iv = (ImageView) convertView
					.findViewById(R.id.sign_unread_iv);
			holder.video_img_iv = (ImageView) convertView
					.findViewById(R.id.video_img_iv);
			holder.isSelected = (CheckBox) convertView
					.findViewById(R.id.notify_is_selected_cb);
			holder.notify_message_title_tv = (TextView) convertView
					.findViewById(R.id.notify_message_title_tv);
			holder.notify_message_content_tv = (TextView) convertView
					.findViewById(R.id.notify_message_content_tv);

			holder.notify_message_time_tv = (TextView) convertView
					.findViewById(R.id.notify_message_time_tv);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		if (mIsManagement.get(0)) {
			holder.isSelected.setVisibility(View.VISIBLE);
		} else {
			holder.isSelected.setVisibility(View.GONE);
		}

		holder.isSelected
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						// 修改当前position的bean信息(选中与否),所有信息以bean中的为准
						if (null != itemList.get(position)) {
							itemList.get(position).setDeleteSelected(isChecked);
						}
					}
				});

		if (null != itemList.get(position)) {
			NotificationBean bean = itemList.get(position);
			holder.notify_message_title_tv.setText(bean.getNotificationName());
			holder.notify_message_content_tv.setText(bean
					.getNotificationContent());
			holder.isSelected.setChecked(bean.isDeleteSelected());
			
			if(bean.getNotificationContentPatternType()
					==NotificationBean.NOTIFICATION_CONTENT_PATTERN_TYPE_IMAGE_TEXT){ // 图文
				holder.notification_item_ll.setPadding(1, 1, 1, 1);
				holder.video_img_iv.setVisibility(View.VISIBLE);
				if (!TextUtils.isEmpty(bean.getNotificationIconUrl())) {
					ImageDownloadModule.getInstance().download(
							bean.getNotificationIconUrl(), R.drawable.default_224_128,
							holder.video_img_iv);
				}else{
					holder.video_img_iv.setImageBitmap(null);
					holder.video_img_iv.setImageDrawable(null);
					holder.video_img_iv.setBackgroundResource(R.drawable.default_224_128);
				}
			}else{
				holder.notification_item_ll.setPadding(30, 1, 1, 1);
				holder.video_img_iv.setVisibility(View.GONE);
			}
			
			if(bean.getNotificationIsRead()){
				holder.sign_unread_iv.setVisibility(View.GONE);
			}else{
				holder.sign_unread_iv.setVisibility(View.VISIBLE);
			}
			String formatDate = FoneUtil.formatDate(
					new Date(bean.getNotificationCreateTime()), "yyyy-MM-dd");
			holder.notify_message_time_tv.setText(formatDate);
		}

		return convertView;
	}

	class ViewHolder {
		public TextView notify_message_title_tv, notify_message_content_tv,
				notify_message_time_tv;
		public ImageView video_img_iv;
		public ImageView sign_unread_iv;
		public CheckBox isSelected;
		public LinearLayout notification_item_ll;

	}
}
